package service

import "fmt"

type Content struct {
	Id uint64
	Title string
	Content string
	Addtime string
}

//记事本内容list
func ContentList(page uint64) []Content {
	var content =  []Content{}
	dbConnect()
	defer dbClose()
	var  limit uint64 = 10
	offset := (page - 1) * limit
	dbConn.Offset(offset).Limit(limit).Find(&content)//dbConn可用是因为同属service包
	fmt.Println(content)
	return content
}

//添加一条数据
func CreateNode(content Content) (bool,string){
	dbConnect()
	defer dbClose()
	db := dbConn.Create(&content)
	if db.Error != nil{
		return false,db.Error.Error()
	}
	return true,""
}

//删除一条
func DelNode(id uint64) (bool,string){
	dbConnect()
	var content =  Content{}
	defer dbClose()
	dbConn.Where("id = ? ",id).Find(&content)
	if content.Id > 0 {
		db := dbConn.Delete(&content)
		if db.Error != nil{
			return false,db.Error.Error()
		}
		return true,""
	}
	return false,"您要删除的数据不存在"

}


//更新一条
func UpdateNode(content Content) (bool,string){
	dbConnect()
	defer dbClose()
	var contentExists =  Content{}
	dbConn.Where("id = ? ",content.Id).Find(&contentExists)
	if contentExists.Id > 0 {
		contentExists.Content = content.Content
		contentExists.Title = content.Title
		db := dbConn.Model(&contentExists).Update(contentExists)
		//db := dbConn.Update(&contentExists)
		if db.Error != nil{
			return false,db.Error.Error()
		}
		return true,"更新成功"
	}
	return false,"您要更新的数据不存在"

}


